package xyz.jason.mybatisgenerator.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @ProjectName: mybatis-generator
 * @Package: xyz.jason.mybatisgenerator
 * @ClassName: Controller
 * @Author: Jason · L
 * @Description: 登录页面
 * @Date: 2021/4/6 14:17
 * @Version: 1.0
 */
@Controller
public class LoginController {

    @Autowired
    Producer captcha;

    @GetMapping("index")
    public String login(){
        return "index.html";
    }

    @PostMapping("doLogin")
    @ResponseBody
    public String doLogin(){
       return "OK";
    }



    @GetMapping("/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletOutputStream out = null;
        try{
            response.setContentType("image/jpg");
            String capText = captcha.createText();
            request.getSession().setAttribute("captcha",capText);
            BufferedImage image = captcha.createImage(capText);
            out = response.getOutputStream();
            ImageIO.write(image,"jpg",out);
        }finally {
            if(out != null){
                out.flush();
                out.close();
            }
        }
    }


}
